//
//  AWPlaceholderTextView.m
//  awu
//
//  Created by Hubery on 15/11/3.
//  Copyright © 2015年 ifly. All rights reserved.
//

#import "AWPlaceholderTextView.h"

@interface AWPlaceholderTextView()<UITextViewDelegate>
@property (nonatomic, weak) UILabel *placehoderLabel;
@end

@implementation AWPlaceholderTextView
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addSubviews];
    }
    return self;
}


-(void)addSubviews
{
    self.backgroundColor = [UIColor clearColor];
    //        self.automaticallyAdjustsScrollViewInsets = NO;
    UILabel *placehoderLabel = [[UILabel alloc] init];
    placehoderLabel.numberOfLines = 0;
    placehoderLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:placehoderLabel];
    self.placehoderLabel = placehoderLabel;
    self.placehoderColor = [UIColor lightGrayColor];
    self.font = [UIFont systemFontOfSize:14];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];

}



- (id)initWithFrame:(CGRect)frame
{
    self.delegate = self;
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
//        self.automaticallyAdjustsScrollViewInsets = NO;
        UILabel *placehoderLabel = [[UILabel alloc] init];
        placehoderLabel.numberOfLines = 0;
        placehoderLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:placehoderLabel];
        self.placehoderLabel = placehoderLabel;
        self.placehoderColor = [UIColor lightGrayColor];
//        self.textColor = [UIColor lightGrayColor];
        self.font = [UIFont systemFontOfSize:16];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

-(void)textViewDidChange:(UITextView *)textView
{
    //    textview 改变字体的行间距
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    int lineSpacing;
//    if (((int)[UIScreen mainScreen].bounds.size.height)/100 == 5) {
//        lineSpacing = 14;
//    }
//    else if(((int)[UIScreen mainScreen].bounds.size.height)/100 == 6)
//    {
//        lineSpacing = 24;
//    }
//    else if(((int)[UIScreen mainScreen].bounds.size.height)/100 == 4)
//    {
//        lineSpacing = 5;
//    }
//    else if(((int)[UIScreen mainScreen].bounds.size.height)/100 == 7){
//        lineSpacing = 30;
//    }
//    paragraphStyle.lineSpacing = lineSpacing;// 字体的行间距
    
//    NSDictionary *attributes = @{
//                                 NSFontAttributeName:[UIFont systemFontOfSize:16],
//                                 NSParagraphStyleAttributeName:paragraphStyle
//                                 };
//    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}

- (CGRect)caretRectForPosition:(UITextPosition *)position{
    CGRect originalRect = [super caretRectForPosition:position];
    originalRect.size.height = self.font.lineHeight + 2;
    originalRect.size.width = 2;
    return originalRect;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 监听文字改变
- (void)textDidChange
{
    self.placehoderLabel.hidden = (self.text.length != 0);
}

#pragma mark - 公共方法
- (void)setText:(NSString *)text
{
    [super setText:text];
    
    [self textDidChange];
}

- (void)setPlacehoder:(NSString *)placehoder
{
    _placehoder = [placehoder copy];
    self.placehoderLabel.text = placehoder;
    [self setNeedsLayout];
}

- (void)setPlacehoderColor:(UIColor *)placehoderColor
{
    _placehoderColor = placehoderColor;
    self.placehoderLabel.textColor = placehoderColor;
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placehoderLabel.font = font;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.placehoderLabel.y = 8.0;
    self.placehoderLabel.x = 2.0;
    self.placehoderLabel.width = self.width - 2 * self.placehoderLabel.frame.origin.x;
    CGSize placehoderSize = [self SizeWithString:self.placehoder font:self.placehoderLabel.font];
    self.placehoderLabel.height = placehoderSize.height;
}
-(CGSize)SizeWithString:(NSString*)str font:(UIFont *)font
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    CGSize maxSize = CGSizeMake(self.placehoderLabel.width, MAXFLOAT);
    CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}

@end
